const webpack = require("webpack");
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const path = require("path");
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');

const ENVIRONMENT = process.env.NODE_ENV || 'local';

module.exports = {
  entry: {
    app: ['./src/index.js'],
    vendor: [
      'isomorphic-fetch',
      'babel-polyfill',
      'react',
      'react-dom',
      'react-router',
      'redux',
      'react-redux',
      'react-router-redux',
      'redux-logger',
      'redux-thunk'
    ]
  },
  output: {
    filename: '[name]-[hash:6].js',
    libraryTarget: 'umd',
    publicPath: '/',
    path: path.resolve(__dirname, "../dist")
  },
  devtool: 'cheap-source-map',
  resolve: {
    modules: [
      'src/lib',
      'src/scripts',
      'src/assets',
      'node_modules'
    ]
  },
  module: {
    rules: [{
      enforce: 'pre',
      test: /\.js$/,
      exclude: /node_modules\/(?!object-assign-deep)(?!cast-viewer)/,
      loader: 'eslint-loader',
      options: {
        fix: true
      }
    }, {
      test: /\.js$/,
      exclude: /node_modules\/(?!object-assign-deep)(?!cast-viewer)/,
      use: [
        {
          loader: 'babel-loader'
        }
      ]
    }, {
      test: /\.(png|jpg|ico)$/,
      loader: 'file-loader',
      query: {
        outputPath: 'assets/images/',
        name: '[name].[ext]'
      }
    }, {
      test: /\.css$/,
      use: ExtractTextPlugin.extract({
        fallback: 'style-loader',
        use: 'css-loader'
      })
    }, {
      test: /\.s[a|c]ss$/,
      exclude: /(node_modules|bower_components)/,
      use: ExtractTextPlugin.extract({
        fallback: 'style-loader',
        use: [
          'css-loader',
          'sass-loader'
        ]
      })
    }, {
      test: /\.(eot|svg|ttf|woff|woff2)$/,
      loader: 'file-loader',
      query: {
        outputPath: 'assets/fonts/',
        name: '[name].[ext]'
      }
    }]
  },
  plugins: [
    new HtmlWebpackPlugin({
      title: 'Hyper/Verse',
      filename: 'index.html',
      template: 'src/index.html',
      favicon: 'src/assets/image/favicon.ico'
    }),
    new webpack.HashedModuleIdsPlugin(),
    new webpack.optimize.CommonsChunkPlugin({
      name: 'vendor',
    }),
    new webpack.optimize.CommonsChunkPlugin({
      name: 'manifest',
      minChunks: Infinity
    }),
    new webpack.DefinePlugin({
      APP_CONFIG: JSON.stringify(require('./app.config.js'))
    }),
    new webpack.ProvidePlugin({
      React: 'react',
      Redux: 'redux',
      ReactRedux: 'react-redux',
      ReactDOM: 'react-dom',
      ReactRouter: 'react-router',
      Hfetch: 'Hfetch',
      Util: 'Util'
    }),
    new ExtractTextPlugin({
      filename: 'app-[hash:6].css',
      disable: ENVIRONMENT === 'local',
      allChunks: true
    }),
    new CopyWebpackPlugin([
      {
        context: 'node_modules/cast-viewer/textures',
        from: '**/*',
        to: 'textures'
      }
    ])
  ]
};
